/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.processor;

import com.lvyh.lightframe.job.admin.bean.JobAlarmConfigBean;
import com.lvyh.lightframe.job.admin.bean.JobAlarmLogBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskDefBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskHistoryBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskHistoryDetailBean;
import com.lvyh.lightframe.job.admin.bean.JobUserBean;
import com.lvyh.lightframe.job.admin.bean.JobUserPermitBean;
import com.lvyh.lightframe.job.admin.pagination.DefaultPageResult;
import com.lvyh.lightframe.job.admin.pagination.PageParam;
import com.lvyh.lightframe.job.admin.service.JobMainDataService;
import com.lvyh.lightframe.job.enums.AlarmStatus;
import com.lvyh.lightframe.job.enums.AlarmType;
import com.lvyh.lightframe.job.enums.TaskStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lvyh 2021/04/06.
 */
@Component
public class JobManageProcessor {
    @Autowired
    private JobMainDataService jobMainDataService;

    public boolean deleteJobTaskDefBean(int taskType, String taskName) throws Exception {
        JobTaskDefBean existNode = getJobTaskDefInfo(taskType, taskName);
        return jobMainDataService.deleteJobTaskDef(existNode.getId());
    }

    public boolean updateJobTaskDefBean(JobTaskDefBean param) throws Exception {
        JobTaskDefBean jobTaskDefBean = jobMainDataService.selectJobTaskDefById(param.getId());
        if (jobTaskDefBean != null) {
            BeanUtils.copyProperties(param, jobTaskDefBean);
            jobMainDataService.updateJobTaskDef(jobTaskDefBean);
        }
        return true;
    }

    public boolean updateJobTaskHistoryDetailBean(JobTaskHistoryDetailBean param) throws Exception {
        JobTaskHistoryDetailBean updateBean = jobMainDataService.selectJobTaskHistoryDetailById(param.getId());
        if (updateBean != null) {
            BeanUtils.copyProperties(param, updateBean);
            jobMainDataService.updateJobTaskHistoryDetail(updateBean);
        }
        return true;
    }

    public boolean updateJobTaskHistoryBean(JobTaskHistoryBean param) throws Exception {
        JobTaskHistoryBean updateBean = jobMainDataService.selectJobTaskHistoryById(param.getId());
        if (updateBean != null) {
            BeanUtils.copyProperties(param, updateBean);
            jobMainDataService.updateJobTaskHistory(updateBean);
        }
        return true;
    }


    public JobTaskDefBean selectJobTaskDefById(int id) throws Exception {
        return jobMainDataService.selectJobTaskDefById(id);
    }

    public JobTaskDefBean getJobTaskDefInfo(int taskType, String taskName) throws Exception {
        JobTaskDefBean condition = new JobTaskDefBean();
        condition.setTaskName(taskName);
        condition.setTaskType(taskType);
        List<JobTaskDefBean> result = jobMainDataService.selectJobTaskDef(condition);
        return !CollectionUtils.isEmpty(result) ? result.get(0) : null;
    }

    public JobTaskDefBean getJobTaskDefById(int taskId) throws Exception {
        return jobMainDataService.selectJobTaskDefById(taskId);
    }


    public List<JobTaskDefBean> getJobTaskDefList(String taskName) throws Exception {
        JobTaskDefBean condition = new JobTaskDefBean();
        condition.setTaskName(taskName);
        return jobMainDataService.selectJobTaskDef(condition);
    }

    public int saveJobAlarmConfigBean(JobAlarmConfigBean jobAlarmConfigBean) throws Exception {
        return jobMainDataService.saveJobAlarmConfig(jobAlarmConfigBean);
    }

    public int saveJobTaskHistoryBean(JobTaskHistoryBean jobTaskHistoryBean) throws Exception {
        return jobMainDataService.saveJobTaskHistory(jobTaskHistoryBean);
    }

    public int saveJobTaskHistoryDetailBean(JobTaskHistoryDetailBean jobTaskHistoryDetailBean) throws Exception {
        return jobMainDataService.saveJobTaskHistoryDetail(jobTaskHistoryDetailBean);
    }

    public int saveJobAlarmLogBean(JobAlarmLogBean jobAlarmLogBean) throws Exception {
        return jobMainDataService.saveJobAlarmLog(jobAlarmLogBean);
    }

    public JobAlarmConfigBean getJobAlarmConfigInfo(int objectId) throws Exception {
        JobAlarmConfigBean condition = new JobAlarmConfigBean();
        condition.setObjectId(objectId);
        List<JobAlarmConfigBean> result = jobMainDataService.selectJobAlarmConfig(condition);
        return !CollectionUtils.isEmpty(result) ? result.get(0) : null;
    }

    public List<JobAlarmConfigBean> getJobAlarmConfigByType(int AlarmType) throws Exception {
        JobAlarmConfigBean condition = new JobAlarmConfigBean();
        condition.setAlarmType(AlarmType);
        return jobMainDataService.selectJobAlarmConfig(condition);
    }

    public JobAlarmConfigBean getSystemAlarmConfigInfo() throws Exception {
        JobAlarmConfigBean condition = new JobAlarmConfigBean();
        condition.setAlarmType(AlarmType.SYS.getKey());
        condition.setStatus(AlarmStatus.ENABLED.getStatus());
        List<JobAlarmConfigBean> result = jobMainDataService.selectJobAlarmConfig(condition);
        return !CollectionUtils.isEmpty(result) ? result.get(0) : null;
    }


    public boolean updateJobAlarmConfigBean(JobAlarmConfigBean param) throws Exception {
        JobAlarmConfigBean jobAlarmConfigBean = jobMainDataService.selectJobAlarmConfigById(param.getId());
        if (jobAlarmConfigBean != null) {
            BeanUtils.copyProperties(param, jobAlarmConfigBean);
            jobMainDataService.updateJobAlarmConfig(jobAlarmConfigBean);
        }
        return true;
    }

    public List<JobTaskDefBean> getJobTaskDefList() throws Exception {
        JobTaskDefBean condition = new JobTaskDefBean();
        condition.setStatus(TaskStatus.ENABLED.getStatus());
        return jobMainDataService.selectJobTaskDef(condition);
    }

    public List<JobTaskHistoryBean> getJobTaskHistoryList() throws Exception {
        JobTaskHistoryBean condition = new JobTaskHistoryBean();
        return jobMainDataService.selectJobTaskHistory(condition);
    }

    public int saveJobTaskDefBean(JobTaskDefBean jobTaskDefBean) throws Exception {
        return jobMainDataService.saveJobTaskDef(jobTaskDefBean);
    }


    public JobUserBean getJobUserInfo(String username) throws Exception {
        JobUserBean condition = new JobUserBean();
        condition.setUserName(username);
        List<JobUserBean> result = jobMainDataService.selectJobUser(condition);
        return !CollectionUtils.isEmpty(result) ? result.get(0) : null;
    }

    public boolean updateJobUserBean(JobUserBean param) throws Exception {
        JobUserBean bean = jobMainDataService.selectJobUserById(param.getId());
        if (bean != null) {
            BeanUtils.copyProperties(param, bean);
            jobMainDataService.updateJobUser(bean);
        }
        return true;
    }

    public boolean deleteJobUserBean(String username) throws Exception {
        JobUserBean existNode = this.getJobUserInfo(username);
        return jobMainDataService.deleteJobUser(existNode.getId());
    }

    public List<JobUserBean> pageList(int offset, int pagesize, String username) throws Exception {
        List<JobUserBean> list = new ArrayList<>();
        JobUserBean condition = new JobUserBean();
        condition.setUserName(username);

        int pageNum = offset <= pagesize ? 1 : offset / pagesize + 1;
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(pageNum);
        pageParam.setPageSize(pagesize);
        pageParam.setOrderColumns("update_time desc");
        DefaultPageResult<JobUserBean> pageInfo = jobMainDataService.selectJobUserByCondition(condition, pageParam);

        if (pageInfo != null && !CollectionUtils.isEmpty(pageInfo.getResultList())) {
            return pageInfo.getResultList();
        }
        return list;
    }

    public Integer pageListCount(String username) throws Exception {
        JobUserBean condition = new JobUserBean();
        condition.setUserName(username);
        return jobMainDataService.countJobUserByCondition(condition);
    }


    public boolean deleteJobUserPermitBean(int taskId, int userId) throws Exception {
        JobUserPermitBean existNode = getJobUserPermitInfo(taskId, userId);
        return jobMainDataService.deleteJobUserPermit(existNode.getId());
    }

    public boolean updateJobUserPermitBean(JobUserPermitBean param) throws Exception {
        JobUserPermitBean JobUserPermitBean = jobMainDataService.selectJobUserPermitById(param.getId());
        if (JobUserPermitBean != null) {
            BeanUtils.copyProperties(param, JobUserPermitBean);
            jobMainDataService.updateJobUserPermit(JobUserPermitBean);
        }
        return true;
    }

    public JobUserPermitBean getJobUserPermitInfo(int taskId, int userId) throws Exception {
        JobUserPermitBean condition = new JobUserPermitBean();
        condition.setTaskId(taskId);
        condition.setUserId(userId);
        List<JobUserPermitBean> result = jobMainDataService.selectJobUserPermit(condition);
        return !CollectionUtils.isEmpty(result) ? result.get(0) : null;
    }

    public int saveJobUserPermitBean(JobUserPermitBean JobUserPermitBean) throws Exception {
        return jobMainDataService.saveJobUserPermit(JobUserPermitBean);
    }

    public List<JobUserPermitBean> getJobUserPermitList() throws Exception {
        JobUserPermitBean condition = new JobUserPermitBean();
        return jobMainDataService.selectJobUserPermit(condition);
    }


}
